import { createRouter, createWebHistory } from 'vue-router'
import store from '../store'
import { ElMessageBox } from 'element-plus';


const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('../views/HomeVue.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/LoginVue.vue')
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('../views/RegisterVue.vue')
  },
  {
    path: '/goods',
    name: 'goods',
    component: () => import('../views/GoodsVue.vue')
  },
  {
    path: '/detail',
    name: 'detail',
    component: () => import('../views/DetailPage.vue')
  },
  {
    path: '/recommend',
    name: 'recommend',
    component: () => import('../views/RecommendVue.vue'),
  },
  {
    path: '/search',
    name: 'search',
    component: () => import('../views/SearchVue.vue'),
  },
  {
    path: '/cart',
    name: 'cart',
    component: () => import('../views/CartVue.vue'),
    meta: {
      requireLogin: true,
    }
  },
  {
    path: '/my_orders',
    name: 'my_orders',
    component: () => import('../views/MyOrdersVue.vue'),
    meta: {
      requireLogin: true,
    }
  },
  {
    path: '/oneself',
    name: 'oneself',
    component: () => import('../views/OneselfVue.vue'),
    meta: {
      requireLogin: true,
    }
  },
  {
    path: '/order',
    name: 'order',
    component: () => import('../views/OrderVue.vue'),
    meta: {
      requireLogin: true,
    }
  },
  {
    path: '/admin_user',
    name: 'admin_user',
    component: () => import('../views/adminViews/AdminUserVue.vue'),
    meta: {
      requireLogin: true,
      requireAdmin: true,
    }
  },
  {
    path: '/admin_goods',
    name: 'admin_goods',
    component: () => import('../views/adminViews/AdminGoodsVue.vue'),
    meta: {
      requireLogin: true,
      requireAdmin: true,
    }
  },
  {
    path: '/admin_Cart',
    name: 'admin_Cart',
    component: () => import('../views/adminViews/AdminCartVue.vue'),
    meta: {
      requireLogin: true,
      requireAdmin: true,
    }
  },
  {
    path: '/admin_order',
    name: 'admin_order',
    component: () => import('../views/adminViews/AdminOrderVue.vue'),
    meta: {
      requireLogin: true,
      requireAdmin: true,
    }
  },
  {
    path: '/admin_orderItem',
    name: 'admin_orderItem',
    component: () => import('../views/adminViews/AdminOrderItemVue.vue'),
    meta: {
      requireLogin: true,
      requireAdmin: true,
    }
  },
  {
    path: '/admin_type',
    name: 'admin_type',
    component: () => import('../views/adminViews/AdminTypeVue.vue'),
    meta: {
      requireLogin: true,
      requireAdmin: true,
    }
  },
  {
    path: '/admin_recommend',
    name: 'admin_recommend',
    component: () => import('../views/adminViews/AdminRecommendVue.vue'),
    meta: {
      requireLogin: true,
      requireAdmin: true,
    }
  },
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from) => {
  if (to.meta.requireLogin) {
    if (!store.state.isLogin) {
      ElMessageBox.alert('此操作需要登录才能进行，是否前往登录?', '提示', {
        confirmButtonText: '前往登录',
        callback: (action) => {
          if (action === "confirm") {
            store.state.navActive = '/login';
          }
        },
      })
      store.state.navActive = from.path;
    } else if (store.state.isLogin) {
      if (to.meta.requireAdmin) {
        if (store.state.user.isAdmin && store.state.onAdmin) {
          return true;
        } else {
          store.state.navActive = from.path;
        }
      } else {
        return true;
      }
    }
  }
  return true;
})

export default router
